#!/usr/bin/env python
##-----------------------------------------------------------------------------
##  File:   test_tex_iter.py
##  Author: James Mynderse
##-----------------------------------------------------------------------------

from tex_iter import word_iter 
import re

filename = 'test.tex'
newfile = 'test.text'
fout = open(newfile,'w')

isDoc = False
isEq = False
isFi = False
isTa = False
for filename, line_number, word_number, word in word_iter(filename):
    print filename, line_number, word_number, word
    
    ## Process existing environment
    if isEq:
        continue
    if isFi:
        continue
    if isTa:
        continue
        
    ## Begin new environment
    re_begin = r'\\begin\{(?P<content>.[^}])\}'
    obj = re.search(re_begin,word)
    if obj:
        print 'found something'
        environment = obj.group('content').lower()
        print environment
        if document:
            isDoc = True
        elif environment == 'equation':
            isEq = True
        elif figure:
            isFi = True
        elif table:
            isTa = True
        continue

    ## End current environment
    re_end = r'\\end\{(?P<content>.[^}])\}'
    obj = re.search(re_end,word)
    if obj:
        print 'found something'
        environment = obj.group('content').lower()
        
        if document:
            isDoc = False
        elif environment == 'equation':
            isEQ = False
        elif figure:
            isFi = False
        elif table:
            isTa = False
        continue

    ## Strip any \emph{
    re_emph = r'\\emph\{'
    word = re.sub(re_emph,'',word)
    
    ## Strip any lonely }
    re_rbrace = r'\}'
    word = re.sub(re_rbrace,'',word)
    
    fout.write(word)
    fout.write(' ')
    